package com.flower.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.flower.domain.dto.farmer.FlowerDetailDTO;
import com.flower.domain.entity.FlowerCulture;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface FlowerCultureMapper extends BaseMapper<FlowerCulture> {
    
    /**
     * 获取花卉列表
     *
     * @return 花卉列表
     */
    @Select("SELECT f.id, f.name, f.scientific_name as scientificName, f.family, f.genus, " +
            "f.alias, f.origin, f.right_place as rightPlace, f.introduction, " +
            "f.features, f.cultivation_difficulty as cultivationDifficulty, " +
            "f.thumbnail, f.view_count as viewCount, f.create_time as createTime, " +
            "f.update_time as updateTime, f.data_source as dataSource " +
            "FROM flower_encyclopedia f " +
            "ORDER BY f.update_time DESC")
    List<FlowerDetailDTO> selectFlowerList();
    
    /**
     * 获取花卉详情
     *
     * @param id 花卉ID
     * @return 花卉详情
     */
    @Select("SELECT f.id, f.name, f.scientific_name as scientificName, f.family, f.genus, " +
            "f.alias, f.origin, f.right_place as rightPlace, f.introduction, " +
            "f.features, f.cultivation_difficulty as cultivationDifficulty, " +
            "f.thumbnail, f.view_count as viewCount, f.create_time as createTime, " +
            "f.update_time as updateTime, f.data_source as dataSource " +
            "FROM flower_encyclopedia f " +
            "WHERE f.id = #{id}")
    FlowerDetailDTO selectFlowerDetail(@Param("id") Long id);
    
    /**
     * 增加访问量
     *
     * @param id 花卉ID
     */
    @Update("UPDATE flower_encyclopedia SET view_count = view_count + 1 WHERE id = #{id}")
    void incrementViewCount(@Param("id") Long id);
    
    /**
     * 搜索花卉
     *
     * @param keyword 关键词
     * @return 花卉列表
     */
    @Select("SELECT f.id, f.name, f.scientific_name as scientificName, f.family, f.genus, " +
            "f.alias, f.origin, f.right_place as rightPlace, f.introduction, " +
            "f.features, f.cultivation_difficulty as cultivationDifficulty, " +
            "f.thumbnail, f.view_count as viewCount, f.create_time as createTime, " +
            "f.update_time as updateTime, f.data_source as dataSource " +
            "FROM flower_encyclopedia f " +
            "WHERE f.name LIKE CONCAT('%', #{keyword}, '%') " +
            "OR f.scientific_name LIKE CONCAT('%', #{keyword}, '%') " +
            "OR f.alias LIKE CONCAT('%', #{keyword}, '%') " +
            "OR f.introduction LIKE CONCAT('%', #{keyword}, '%') " +
            "ORDER BY f.update_time DESC")
    List<FlowerDetailDTO> searchFlowers(@Param("keyword") String keyword);
} 